
var utils = {

    //获取页面指定key值的参数值
    getParamer: function (key) {
        var map = this.getSearch();
        return map[key];
    },
    //获取页面所有key的参数值的集合
    getSearch: function () {
        var search = decodeURIComponent(window.location.search);
        if (search != "") {
            search = search.substring(1);
        } else {
            return {};
        }
        var strsz = search.split("&");
        var map = {};
        $.each(strsz, function (n, strs) {
            if (strs.indexOf("=") != -1) {
                var tempsz = strs.split("=");
                var tempkey = tempsz[0];
                var tempvalue = tempsz[1];
                map[tempkey] = tempvalue;
            }
        });
        return map;
    },

    //获取当前网址的host地址
    getHost: function () {
        var js="utils.js";
        var scripts = document.getElementsByTagName("script");
        var path = "";
        for (var i = 0, l = scripts.length; i < l; i++) {
            var src = scripts[i].src;
            if (src.indexOf(js) != -1) {
                path = src;
                break;
            }
        }
        var ss = path.split("/");
        ss.length = ss.length - 4;
        path = ss.join("/");
        return path;
    },

    //当指定data绑定到页面表单
    setFormData: function (select, data) {
        debugger
        if(data == null){
            return;
        }
        var inputs = $(select + " :input");
        $.each(inputs, function (i, input) {
            var val = data[$(input).attr("name")];

            if (val != null) {
                if ($(input).attr("type") == "checkbox" || $(input).attr("type") == "radio") {
                    debugger
                    if ($(input).val() == val + "") {
                        $(input)[0].checked = true;
                    }
                    return true;
                }
                if($(input).attr("type") == "select"){
                    $("[name='"+input+"'] option[value='"+data.input+"']").attr("selected","selected");
                    return true;
                }
                $(input).val(val);
            }
        });
    },

    //只读回显
    setFormReadOnly: function (select, data) {
        debugger
        if(data == null){
            return;
        }
        var inputs = $(select + " :input");
        $.each(inputs, function (i, input) {
            var val = data[$(input).attr("name")];
            if (val != null) {
                if ($(input).attr("type") == "checkbox" || $(input).attr("type") == "radio") {
                    if ($(input).val() == val + "") {
                        $(input)[0].checked = true;
                    }
                    return true;
                }
                if($(input).attr("type") == "select"){
                    $("[name='"+input+"'] option[value='"+data.input+"']").attr("selected","selected");
                    return true;
                }
                $(input).val(val);
                $(input).attr("disabled","disabled").attr("readonly","readonly");;
            }
        });
    },

    //获取指定key值的cookie值
    getCookie: function (name) {
        var arr, reg = new RegExp("(^| )utils=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg)) {
            var mapStr = decodeURIComponent(arr[2]);
            if(mapStr==null){
                mapStr = "{}";
            }
            var map = JSON.parse(mapStr);
            return map[name];
        } else {
            return null;
        }
    },




}
